import os
from os.path import join, abspath
env = Environment(ENV=os.environ)

if len(COMMAND_LINE_TARGETS) > 0:
	target = COMMAND_LINE_TARGETS[0].upper()
else:
	target = 'ALL'


if env['PLATFORM'] == 'win32':
	javahome = r'C:\Program Files\Java\jdk1.6.0_18'

	incpath = [join(javahome, 'include'),
		   join(javahome, 'include', 'win32'),
		   r'C:\Program Files\MeCab\sdk',
		   r'C:\Home\lib\protobuf\include']
	
	libpath = [r'C:\Program Files\MeCab\sdk',
		   r'C:\Home\lib\protobuf\lib']
	
	libs = ['libmecab', 'libprotobuf']

	ccflags = (['/DDLL_IMPORT', '/MD', '/EHsc', '/Ox'] +
		   ['/I' + item for item in incpath])
	
	env.AppendUnique(
		CCFLAGS=ccflags,
		LIBPATH=libpath,
		LIBS=libs)
else:
	if env['PLATFORM'] == 'sunos':
		javahome = '/usr/java'
		incpath = [join(javahome, 'include'),
			   join(javahome, 'include', 'solaris')]
	else:
		javahome = '/usr/lib/jvm/java-6-sun'
		incpath = [join(javahome, 'include'),
			   join(javahome, 'include', 'linux')]

	libpath = ['/usr/lib', '/usr/local/lib']
	libs = ['mecab', 'protobuf']

	ccflags = (['-Wall', '-g', '-O2'] +
		   ['-I' + item for item in incpath])
	
	env.AppendUnique(
		CCFLAGS=ccflags,
		LIBPATH=libpath,
		LIBS=libs)


# javah
def do_javah():
	classdir = join('..', 'bin', 'classes')
	impldir = join(classdir, 'net', 'moraleboost', 'mecab', 'impl')
	return env.JavaH(
		target='.',
		source=[join(impldir, 'StandardTagger.class'),
				join(impldir, 'StandardNode.class'),
				join(impldir, 'LocalProtobufTagger.class')],
		JAVACLASSDIR=classdir)

# protoc (C++ & Java)
def do_protoc():
	protopath = abspath(join('..', 'etc', 'protobuf'))
	return env.Command(
		['messages.pb.cc', 'messages.pb.h'],
		join(protopath, 'messages.proto'),
		('protoc --cpp_out=. --java_out=' + join('..', 'src') +
		' "--proto_path=' + protopath + '" ${SOURCE}'))

# shared library
def do_build_library():
	return env.SharedLibrary('CMeCab', ['mecab.cpp'])

# shared library (with protobuf)
def do_build_library_with_protobuf():
	return env.SharedLibrary(
		'CMeCab_protobuf',
		['mecab_protobuf.cpp', 'messages.pb.cc'])

# manifest (Windows only)
def do_embed_manifest(shared_lib, dll_name):
	Clean(shared_lib, dll_name + '.dll.manifest')
	return env.Command(
		'Manifest' + dll_name + ".dll", shared_lib,
		('mt.exe -manifest ${SOURCE}.manifest ' +
		'-outputresource:${SOURCE};2 -nologo'))


# build script
do_javah()
do_protoc()
shared_lib = do_build_library()
shared_lib_protobuf = do_build_library_with_protobuf()

if env['PLATFORM'] == 'win32':
	Default(do_embed_manifest(shared_lib, 'CMeCab'),
			do_embed_manifest(shared_lib_protobuf, 'CMeCab_protobuf'))
else:
	Default(shared_lib, shared_lib_protobuf)

